package com.xiaozhen.data.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/23 15:59
 * @description:
 */
public class DateUtil {

    public static Date parse2Date(String dateString, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    public static Long parseMicTime(String dateString) {
        Date date = parse2Date(dateString, "yyyyMMddHHmmss");
        if (date == null) {
            return 0L;
        }
        // 微秒
        Long cuTime = date.getTime() * 1000;
        // 纳秒
        Long nanoTime = System.nanoTime();
        return cuTime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
    }


    public static Long parseMicTime(Date date) {
        // 微秒
        Long cuTime = date.getTime() * 1000;
        // 纳秒
        Long nanoTime = System.nanoTime();
        return cuTime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
    }

    /**
     * 获取毫秒， 网上百度都有
     */
    public static Long getMicTime() {
        // 微秒
        Long cuTime = System.currentTimeMillis() * 1000;
        // 纳秒
        Long nanoTime = System.nanoTime();
        return cuTime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
    }
}
